#include "StdAfx.h"
#include "HPPool.h"


HPPool::HPPool(void)
{
	this->retain();
	this->pArray = NULL;
	this->uLen = 0;
	this->uTotal = 0;
}

HPPool::HPPool(unsigned long long uLength)
{
	this->retain();
	this->createPool(uLength);
}

HPPool::~HPPool(void)
{
	this->destroyPool();
}

void HPPool::createPool(unsigned long long uLength)
{
	this->uLen = uLength;
	this->pArray = new void*[uLength];
	this->uTotal = 1;
}

void HPPool::destroyPool()
{
	delete(this->pArray);
}

 unsigned long long HPPool::inPool(void* p)
{
	WaitForSingleObject(hLock,INFINITE);
	if(uTotal > uLen)
	{
		ReleaseMutex(hLock);
		return 0;
	}
	else
	{
		((void**)pArray)[uTotal] = p;
		uTotal ++;
		ReleaseMutex(hLock);
		return uTotal-1;
	}
}

void *HPPool::outPool()
{
	WaitForSingleObject(hLock,INFINITE);
	if(uTotal <= 1)
	{
		ReleaseMutex(hLock);
		return NULL;
	}
	else
	{
		uTotal --;
		ReleaseMutex(hLock);
		return ((void**)pArray)[uTotal];
	}
}

void *HPPool::outPoolByFlag(unsigned long long uFlag)
{
	WaitForSingleObject(hLock,INFINITE);
	if(uFlag >= uTotal || uFlag == 0)
	{
		ReleaseMutex(hLock);
		return NULL;
	}
	else
	{
		uTotal --;
		((void**)pArray)[uFlag] = ((void**)pArray)[uTotal];
		ReleaseMutex(hLock);
		return ((void**)pArray)[uFlag];
	}
}

void *HPPool::getElemByFlag(unsigned long long uFlag)
{
	WaitForSingleObject(hLock,INFINITE);
	if(uFlag >= uTotal || uFlag == 0)
	{
		ReleaseMutex(hLock);
		return NULL;
	}
	else
	{
		ReleaseMutex(hLock);
		return ((void**)pArray)[uFlag];
	}
}

unsigned long long HPPool::getTotal()
{
	return uTotal;
}